using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SBPweb.Themes.Windows;
using SBPweb.Themes.Windows.Skins;

namespace SBPweb.Controls.Windows.Editors
{
	/// <summary>
	/// Class of TextBox
	/// </summary>
	[ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
	public partial class TextBox : System.Windows.Forms.TextBox, ISkinnable
	{
		#region Private members

		private bool mUseDefaultSkin;
		private Skin mSkin;
		private Color mNoSkinBackColor;
		private Color mNoSkinForeColor;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Constructor of TextBox
		/// </summary>
		public TextBox()
		{
			mNoSkinBackColor = BackColor;
			mNoSkinForeColor = ForeColor;

			SetStyle();
			InitializeComponent();
			RegisterEventHandlers();
			DoInvalidate();

			MaxLength = 1024;
		}

		/// <summary>
		/// Constructor of TextBox
		/// </summary>
		/// <param name="skin"></param>
		public TextBox(Skin skin)
			: base()
		{
			mSkin = skin;
		}

		private void SetStyle()
		{
			base.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
		}

		private void DoInvalidate()
		{
			if (Skin != null)
			{
				if (this.Enabled)
				{
					BackColor = Skin.CachedProperties.ControlColor;
					ForeColor = Skin.CachedProperties.ControlTextColor;
				}
				else
				{
					BackColor = Skin.CachedProperties.ControlDisabledColor;
					ForeColor = Skin.CachedProperties.ControlDisabledTextColor;
				}
			}
			else
			{
				BackColor = mNoSkinBackColor;
				ForeColor = mNoSkinForeColor;
			}
			Invalidate(true);

			try
			{
				OnPaint(new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
			}
			catch (ObjectDisposedException)
			{
			}
		}

		#endregion

		#region Custom draw

		/// <summary>
		/// OnPaint
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnPaint(PaintEventArgs e)
		{
			if (Skin != null)
			{
				e.Graphics.TextRenderingHint = Skin.TextRenderingHint;
			}

			base.OnPaintBackground(e);
			base.OnPaint(e);
		}

		#endregion

		#region Events

		private void RegisterEventHandlers()
		{
			DefaultLookAndFeel.OnSkinChanged += new EventHandler(DefaultLookAndFeel_OnSkinChanged);
			base.EnabledChanged += new EventHandler(TextBox_EnabledChanged);
		}

		private void DefaultLookAndFeel_OnSkinChanged(object sender, EventArgs e)
		{
			DoInvalidate();
		}

		private void TextBox_EnabledChanged(object sender, EventArgs e)
		{
			DoInvalidate();
		}

		/// <summary>
		/// Raises when the text changed
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnTextChanged(EventArgs e)
		{
			base.OnTextChanged(e);
			Color c = this.ForeColor;
			Color cc = this.BackColor;
		}
		#endregion

		#region ISkinnable Members

		/// <summary>
		/// Use default skin?
		/// </summary>
		[Category("Look && feel")]
		public bool UseDefaultSkin
		{
			get
			{
				return mUseDefaultSkin;
			}
			set
			{
				mUseDefaultSkin = value;
				DoInvalidate();
			}
		}

		/// <summary>
		/// Used skin
		/// </summary>
		[Category("Look && feel")]
		public Skin Skin
		{
			get
			{
				if (this.UseDefaultSkin)
				{
					return DefaultLookAndFeel.Skin;
				}

				return mSkin;
			}
			set
			{
				mSkin = value;
				DoInvalidate();
			}
		}

		#endregion
	}
}
